#include "predict.h"
#include "ui_predict.h"
#include "detail.h"
#include "../data/company.h"
#include "../utility/connector.h"

Predict::Predict(QWidget *parent, Connector *connector) :
    QWidget(parent),connector(connector),
    ui(new Ui::Predict) {
    qDebug()<<"Predict::Predict called.";
    ui->setupUi(this);
    company=new Company;
    company->entname=new QString;
    company->data=new QVariantMap;
    company->tag=new QStringList;
    ui->tableWidget_result->clearContents();
    QString str1;
    QDateTime time1;
    str1 = "2010-01-01 00:00:00";
    time1 = QDateTime::fromString(str1, "yyyy-MM-dd hh:mm:ss");
    QString str2;
    QDateTime time2;
    str2 = "2999-01-01 00:00:00";
    time2 = QDateTime::fromString(str2, "yyyy-MM-dd hh:mm:ss");
    ui->dateTimeEdit_estdate->setDateTime(time1);
    ui->dateTimeEdit_opfrom->setDateTime(time1);
    ui->dateTimeEdit_opto->setDateTime(time2);
    ui->dateTimeEdit_candate->setDateTime(time2);
    ui->dateTimeEdit_revdate->setDateTime(time2);

    ui->tableWidget_result->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    connect(ui->tableWidget_result,SIGNAL(cellClicked(int,int)),
            this,SLOT(showDetail()));
    connect(ui->commandLinkButton,SIGNAL(clicked()),
            this,SLOT(predict()));
    /* TODO: [Done] 搬砖
     * 和Filter的构造函数类似，需要写大量的connect
     * 把所有输入控件的更新信号都绑定到updateCompany上。A
     * 例子如下。
     */
    connect(ui->lineEdit_bidnum,SIGNAL(textChanged(QString)),
            this,SLOT(updateCompany()));
    connect(ui->lineEdit_regcapcur,SIGNAL(textChanged(QString)),
            this,SLOT(updateCompany()));
    connect(ui->lineEdit_empnum,SIGNAL(textChanged(QString)),
            this,SLOT(updateCompany()));
    connect(ui->lineEdit_branchnum,SIGNAL(textChanged(QString)),
            this,SLOT(updateCompany()));
    connect(ui->lineEdit_taxunpaidnum,SIGNAL(textChanged(QString)),
            this,SLOT(updateCompany()));
    connect(ui->lineEdit_lisubconam,SIGNAL(textChanged(QString)),
            this,SLOT(updateCompany()));
    connect(ui->lineEdit_liacconam,SIGNAL(textChanged(QString)),
            this,SLOT(updateCompany()));
    connect(ui->lineEdit_shopnum,SIGNAL(textChanged(QString)),
            this,SLOT(updateCompany()));
    connect(ui->lineEdit_insurance,SIGNAL(textChanged(QString)),
            this,SLOT(updateCompany()));
    connect(ui->lineEdit_passpercent,SIGNAL(textChanged(QString)),
            this,SLOT(updateCompany()));
    connect(ui->lineEdit_recruitment,SIGNAL(textChanged(QString)),
            this,SLOT(updateCompany()));
    connect(ui->lineEdit_investnum,SIGNAL(textChanged(QString)),
            this,SLOT(updateCompany()));
    connect(ui->lineEdit_pledgenum,SIGNAL(textChanged(QString)),
            this,SLOT(updateCompany()));
    connect(ui->lineEdit_enforce_amount,SIGNAL(textChanged(QString)),
            this,SLOT(updateCompany()));
    connect(ui->lineEdit_ibrand_num,SIGNAL(textChanged(QString)),
            this,SLOT(updateCompany()));
    connect(ui->lineEdit_icopy_num,SIGNAL(textChanged(QString)),
            this,SLOT(updateCompany()));
    connect(ui->lineEdit_ipat_num,SIGNAL(textChanged(QString)),
            this,SLOT(updateCompany()));
    connect(ui->lineEdit_idom_num,SIGNAL(textChanged(QString)),
            this,SLOT(updateCompany()));
    connect(ui->lineEdit_is_punish,SIGNAL(textChanged(QString)),
            this,SLOT(updateCompany()));
    connect(ui->lineEdit_is_except,SIGNAL(textChanged(QString)),
            this,SLOT(updateCompany()));
    connect(ui->lineEdit_is_justice_credit,SIGNAL(textChanged(QString)),
            this,SLOT(updateCompany()));
    connect(ui->lineEdit_changes,SIGNAL(textChanged(QString)),
            this,SLOT(updateCompany()));
    connect(ui->lineEdit_entname,SIGNAL(textChanged(QString)),
            this,SLOT(updateCompany()));

    connect(ui->comboBox_entcat,SIGNAL(currentTextChanged(QString)),
            this,SLOT(updateCompany()));
    connect(ui->comboBox_enttype,SIGNAL(currentTextChanged(QString)),
            this,SLOT(updateCompany()));
    connect(ui->comboBox_is_jnsn,SIGNAL(currentTextChanged(QString)),
            this,SLOT(updateCompany()));
    connect(ui->comboBox_is_kcont,SIGNAL(currentTextChanged(QString)),
            this,SLOT(updateCompany()));
    connect(ui->comboBox_level_rank,SIGNAL(currentTextChanged(QString)),
            this,SLOT(updateCompany()));
    connect(ui->comboBox_fluctuation,SIGNAL(currentTextChanged(QString)),
            this,SLOT(updateCompany()));
    connect(ui->comboBox_industryphy,SIGNAL(currentTextChanged(QString)),
            this,SLOT(updateCompany()));
    connect(ui->comboBox_credit_grade,SIGNAL(currentTextChanged(QString)),
            this,SLOT(updateCompany()));

    connect(ui->radioButton_longterm,SIGNAL(clicked()),
            this,SLOT(updateCompany()));
    connect(ui->radioButton_shortterm,SIGNAL(clicked()),
            this,SLOT(updateCompany()));
    connect(ui->radioButton_is_infoa_no,SIGNAL(clicked()),
            this,SLOT(updateCompany()));
    connect(ui->radioButton_is_infob_no,SIGNAL(clicked()),
            this,SLOT(updateCompany()));
    connect(ui->radioButton_is_infoa_yes,SIGNAL(clicked()),
            this,SLOT(updateCompany()));
    connect(ui->radioButton_is_infob_yes,SIGNAL(clicked()),
            this,SLOT(updateCompany()));

    connect(ui->dateTimeEdit_opto,SIGNAL(dateTimeChanged(QDateTime)),
            this,SLOT(updateCompany()));
    connect(ui->dateTimeEdit_opfrom,SIGNAL(dateTimeChanged(QDateTime)),
            this,SLOT(updateCompany()));
    connect(ui->dateTimeEdit_candate,SIGNAL(dateTimeChanged(QDateTime)),
            this,SLOT(updateCompany()));
    connect(ui->dateTimeEdit_estdate,SIGNAL(dateTimeChanged(QDateTime)),
            this,SLOT(updateCompany()));
    connect(ui->dateTimeEdit_revdate,SIGNAL(dateTimeChanged(QDateTime)),
            this,SLOT(updateCompany()));
    qDebug()<<"Predict::Predict returned.";
}

Predict::~Predict() {
    qDebug()<<"Predict::~Predict called.";
    delete company;
    delete ui;
    qDebug()<<"Predict::~Predict returned.";
}

void Predict::predict() {
    qDebug()<<"Predict::predict called.";
    ui->tableWidget_result->clearContents();
    connector->predict(company,company);
    /* TODO: [Done] 数据上屏
     * 数据：company->tag
     */
    QString str;
    str=(*company->entname);
    ui->tableWidget_result->setItem(0,0,new QTableWidgetItem(str));
    ui->tableWidget_result->item(0,0)->setTextAlignment(Qt::AlignCenter);
    for(int i=0; i<ui->tableWidget_result->columnCount()-1; i++) {
        str=company->tag->value(i);
        QTableWidgetItem *item=new QTableWidgetItem(str);
        ui->tableWidget_result->setItem(0,i+1,item);
        ui->tableWidget_result->item(0,i+1)->setTextAlignment(Qt::AlignCenter);
    }
    ui->tableWidget_result->setItem(0,ui->tableWidget_result->columnCount()-1,new QTableWidgetItem("点击查看"));
    ui->tableWidget_result->item(0,ui->tableWidget_result->columnCount()-1)->setTextAlignment(Qt::AlignCenter);
    ui->tableWidget_result->horizontalHeader()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
    ui->tableWidget_result->horizontalHeader()->setSectionResizeMode(1, QHeaderView::ResizeToContents);
    ui->tableWidget_result->verticalHeader()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
    qDebug()<<"Predict::predict returned.";
}

void Predict::updateCompany() {
    qDebug()<<"Predict::updateCompany called.";
    /* TODO: [Done] 数据下屏
     * 数据：company->data
     */
    *company->entname=ui->lineEdit_entname->text();
    (*company->data)["企业名称"]=ui->lineEdit_entname->text();
    (*company->data)["企业(机构)类型"]=ui->comboBox_enttype->currentText();
    (*company->data)["行业门类"]=ui->comboBox_industryphy->currentText();
    (*company->data)["企业类别"]=ui->comboBox_entcat->currentText();
    (*company->data)["信用等级"]=ui->comboBox_credit_grade->currentText();
    (*company->data)["守合同重信用企业"]=ui->comboBox_is_kcont->currentText();
    if(!QString::compare(ui->comboBox_fluctuation->currentText(), "")) {  //若波动为无，则两个disable
        ui->dateTimeEdit_candate->setEnabled(false);
        ui->dateTimeEdit_revdate->setEnabled(false);
    } else if(!QString::compare(ui->comboBox_fluctuation->currentText(), "吊销")) {  //若吊销，则注销disable
        ui->dateTimeEdit_candate->setEnabled(false);
        ui->dateTimeEdit_revdate->setEnabled(true);
    } else if(!QString::compare(ui->comboBox_fluctuation->currentText(), "注销")) {  //若注销，则吊销disable
        ui->dateTimeEdit_candate->setEnabled(true);
        ui->dateTimeEdit_revdate->setEnabled(false);
    } else {
        ui->dateTimeEdit_candate->setEnabled(true);
        ui->dateTimeEdit_revdate->setEnabled(true);
    }
    (*company->data)["波动经历"]=ui->comboBox_fluctuation->currentText();

    QRadioButton* btn1 = qobject_cast<QRadioButton*>(ui->buttonGroup->checkedButton());
    if(btn1!=nullptr) (*company->data)["著名商标"]=btn1->text();
    QRadioButton* btn2 = qobject_cast<QRadioButton*>(ui->buttonGroup_2->checkedButton());
    if(btn2!=nullptr) (*company->data)["驰名商标"]=btn2->text();
    (*company->data)["专精特新中小企业"]=ui->comboBox_is_jnsn->currentText();
    (*company->data)["企业技术中小级别"]=ui->comboBox_level_rank->currentText();

    (*company->data)["企业创建时间"]=ui->dateTimeEdit_estdate->dateTime().toSecsSinceEpoch();
    if(ui->dateTimeEdit_candate->isEnabled()) {
        (*company->data)["企业注销时间"]=ui->dateTimeEdit_candate->dateTime().toSecsSinceEpoch();
    } else {
        (*company->data)["企业注销时间"]="";            //当注销不可编辑的时候为空
    }
    if(ui->dateTimeEdit_revdate->isEnabled()) {
        (*company->data)["企业吊销时间"]=ui->dateTimeEdit_revdate->dateTime().toSecsSinceEpoch();
    } else {
        (*company->data)["企业吊销时间"]="";            //当吊销不可编辑的时候为空
    }

    (*company->data)["起止"]=ui->dateTimeEdit_opfrom->dateTime().toSecsSinceEpoch();
    (*company->data)["终止"]=ui->dateTimeEdit_opto->dateTime().toSecsSinceEpoch();

    QRadioButton* btn3 = qobject_cast<QRadioButton*>(ui->buttonGroup_3->checkedButton());
    if(btn3!=nullptr) (*company->data)["营业执照有效时间"]=btn3->text();

    (*company->data)["注册资本"]=ui->lineEdit_regcapcur->text();
    (*company->data)["从业人数"]=ui->lineEdit_empnum->text();
    (*company->data)["分支机构数"]=ui->lineEdit_branchnum->text();
    (*company->data)["累计欠税额"]=ui->lineEdit_taxunpaidnum->text();
    (*company->data)["累计认缴额"]=ui->lineEdit_lisubconam->text();
    (*company->data)["累计实缴额"]=ui->lineEdit_liacconam->text();
    (*company->data)["网店个数"]=ui->lineEdit_shopnum->text();
    (*company->data)["参保种类数目"]=ui->lineEdit_insurance->text();
    (*company->data)["产品抽查通过率"]=ui->lineEdit_passpercent->text();
    (*company->data)["招聘记录条数"]=ui->lineEdit_recruitment->text();
    (*company->data)["对外投资次数"]=ui->lineEdit_investnum->text();
    (*company->data)["中标次数"]=ui->lineEdit_bidnum->text();
    (*company->data)["出质股权次数"]=ui->lineEdit_pledgenum->text();
    (*company->data)["执行标的数目"]=ui->lineEdit_enforce_amount->text();
    (*company->data)["商标申请次数"]=ui->lineEdit_ibrand_num->text();
    (*company->data)["软件著作权登记次数"]=ui->lineEdit_icopy_num->text();
    (*company->data)["专利申请次数"]=ui->lineEdit_ipat_num->text();
    (*company->data)["域名知识产权数"]=ui->lineEdit_idom_num->text();
    (*company->data)["行政处罚次数"]=ui->lineEdit_is_punish->text();
    (*company->data)["异常次数"]=ui->lineEdit_is_except->text();
    (*company->data)["黑名单次数"]=ui->lineEdit_is_justice_credit->text();
    (*company->data)["企业信息变更次数"]=ui->lineEdit_changes->text();
    qDebug()<<"Predict::updateCompany returned.";
}

void Predict::showDetail() {
    qDebug()<<"Predict::showDetail called.";
    if(company->tag->isEmpty())
        return;
    Detail *detail=new Detail(company);
    detail->show();
    qDebug()<<"Predict::showDetail returned.";

}
